package controllers;

import java.util.List;

import models.Rubro;

import controllers.CRUD.ObjectType;
import play.db.Model;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;

@With(Secure.class)
public class Clientes extends ArcadiaCRUD {

	public static void list(int page, String search, String searchFields, String orderBy, String order) {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        if (page < 1) {
            page = 1;
        }
        String observacion="";
        String rubro="";
        String subrubro="";
        String where = null;
        if (search!=null && !"".equals(search.trim())){
        	String query = "";
        	String[] filtros = search.split(",");
        	observacion = filtros[0];
        	if (!"".equals(observacion.trim())){
        		query = query.concat(" (upper(observaciones) like '%");
        		query = query.concat(observacion.toUpperCase().trim());
        		query = query.concat("%')");
        	}
        	rubro = filtros[1];
        	if (!"".equals(rubro.trim())){
        		if (!"".equals(query.trim()))
        			query = query.concat(" or ");
        		query = query.concat(" (upper(rubro.nombre) like '%");
        		query = query.concat(rubro.toUpperCase().trim());
        		query = query.concat("%')");
        	}
        	subrubro = filtros[2];
        	if (!"".equals(subrubro.trim())){
        		if (!"".equals(query.trim()))
        			query = query.concat(" or ");
        		query = query.concat(" (upper(subrubro.nombre) like '%");
        		query = query.concat(subrubro.toUpperCase().trim());
        		query = query.concat("%')");
        	}
        	search = null;
        	if (!"".equals(query.trim()))
        		where = query;
        	else
        		where = null;
        }
        List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, where);
        Long count = type.count(search, searchFields, where);
        Long totalCount = type.count(null, null, where);
        try {        
            render(type, objects, count, totalCount, page, orderBy, order, observacion, rubro, subrubro);
        } catch (TemplateNotFoundException e) {
            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
        }
    }
	
}
